package com.trg.flex.dt
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.controls.listClasses.IListItemRenderer;
	
	/**
	 * Dispatched just before a collection change is processed.
	 */
	[Event(name="beforeCollectionChange")]
	/**
	 * Dispatched just after a collection change is processed.
	 */
	[Event(name="afterCollectionChange")]
	public class ExtendedDataGrid extends DataGrid
	{
		public static const BEFORE_COLLECTION_CHANGE:String = "beforeCollectionChange";
		public static const AFTER_COLLECTION_CHANGE:String = "afterCollectionChange";
	    
	    public static const SELECT_MODE_NORMAL:int = 0;
	    public static const SELECT_MODE_SINGLE:int = 1;
	    public static const SELECT_MODE_MULTI:int = 2;
	    
	    protected override function keyDownHandler(event:KeyboardEvent):void {			
			if ((event.keyCode == Keyboard.PAGE_DOWN || event.keyCode == Keyboard.END) && this.dataProvider.length == 0) {											
				event.stopPropagation();								
			} else {
				super.keyDownHandler(event);
			}
		}
		
		override protected function collectionChangeHandler(event:Event):void {
			dispatchEvent(new Event(BEFORE_COLLECTION_CHANGE));
			super.collectionChangeHandler(event);
			dispatchEvent(new Event(AFTER_COLLECTION_CHANGE));
		}
		
		/**
		 * This is used when a selection column is shown in the DataTable. If this property
		 * is set to other than SELECT_MODE_NORMAL, the default selction behavior is overridden.
		 */
		public var uiSelectMode:int;
		
		override protected function moveSelectionVertically(code:uint, shiftKey:Boolean, ctrlKey:Boolean):void {
			if (uiSelectMode == SELECT_MODE_SINGLE || uiSelectMode == SELECT_MODE_MULTI) {
	    		ctrlKey = (!shiftKey && !ctrlKey);
	    	}
	    	
			super.moveSelectionVertically(code, shiftKey, ctrlKey);
		}
		
		override protected function mouseDownHandler(event:MouseEvent):void {
			if (uiSelectMode == SELECT_MODE_MULTI) {
	    		event.ctrlKey = (!event.shiftKey && !event.ctrlKey);
	    	}
	    	lastClickEdit = false;
	    	super.mouseDownHandler(event);
		}
		
		override protected function mouseUpHandler(event:MouseEvent):void {
			if (uiSelectMode == SELECT_MODE_MULTI) {
	    		event.ctrlKey = (!event.shiftKey && !event.ctrlKey);
	    	}
	    	
	    	super.mouseUpHandler(event);
		}
		
		/**
		 * After an editable column is clicked, selectItem() is called again on the row except that the
		 * correct item renderer is not passed in so we can't know which column was clicked. To solve this
		 * we set this flag to true when selectItem() is called during the click. It remains true until
		 * after the click when it selectItem() is called again, so we can check it there. We reset it to
		 * false on mouse down.
		 */
		private var lastClickEdit:Boolean;
		
		/**
		 * We can disable selection by stoping super.selectItem() from being called.
		 * Here we stop selection when an editable column is clicked.
		 */
		override protected function selectItem(item:IListItemRenderer, shiftKey:Boolean, ctrlKey:Boolean, transition:Boolean=true):Boolean {
			if (editable) {
				if (lastClickEdit) return true;
				
				var pos:Point = itemRendererToIndices(item);
				if (DataGridColumn(columns[pos.x]).editable) {
					lastClickEdit = true;
					return true;
				}
			}
			return super.selectItem(item, shiftKey, ctrlKey, transition);
		}
	}
}